<template>
  <div>
    <el-dialog :close-on-click-modal="false" @close="close" :visible.sync="visible" title="注册">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="公司名称" prop="tenantName">
          <el-input v-model="formData.tenantName" placeholder="请输入公司名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入地址" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入联系电话" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicense">
          <el-input v-model="formData.businessLicense" placeholder="请输入营业执照号码" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="法人姓名" prop="ownerName">
          <el-input v-model="formData.ownerName" placeholder="请输入法人姓名" show-word-limit
            clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="法人身份证" prop="ownerIdcard">
          <el-input v-model="formData.ownerIdcard" placeholder="请输入法人身份证号码" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="附件" prop="enclosure">
          <el-upload ref="enclosure" :file-list="enclosurefileList" :action="enclosureAction"
            :before-upload="enclosureBeforeUpload" list-type="picture" accept="image/*">
            <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传不超过 50MB 图片文件，如jpg或png，上传图片应包括法人身份证正反面、营业执照照片</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import { register } from "@/api/register";

  export default {
  inheritAttrs: false,
  components: {},
  props: [],
  data() {
    return {
      visible:false,
      formData: {
        tenantName: undefined,
        address: undefined,
        phone: undefined,
        businessLicense: undefined,
        ownerName: '',
        ownerIdcard: undefined,
        enclosure: null,
      },
      rules: {
        tenantName: [{
          required: true,
          message: '请输入公司名称',
          trigger: 'blur'
        }],
        address: [{
          required: true,
          message: '请输入地址',
          trigger: 'blur'
        }],
        phone: [{
          required: true,
          message: '请输入联系电话',
          trigger: 'blur'
        }],
        businessLicense: [{
          required: true,
          message: '请输入营业执照号码',
          trigger: 'blur'
        }, {
          pattern: /(^(?:(?![IOZSV])[\dA-Z]){2}\d{6}(?:(?![IOZSV])[\dA-Z]){10}$)|(^\d{15}$)/,
          message: '营业执照号码验证错误，请确认！',
          trigger: 'blur'
        }],
        ownerName: [{
          required: true,
          message: '请输入法人姓名',
          trigger: 'blur'
        }],
        ownerIdcard: [{
          required: true,
          message: '请输入法人身份证号码',
          trigger: 'blur'
        }, {
          pattern: /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
          message: '法人身份证验证失败，请确认！',
          trigger: 'blur'
        }],
      },
      enclosureAction: 'https://jsonplaceholder.typicode.com/posts/',
      enclosurefileList: [],
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onOpen() {
      this.visible=true;
    },
    close() {
      this.$emit('update:visible', false)
      this.$refs['elForm'].resetFields()
      this.visible=false;
      this.enclosurefileList=[];
    },
    handelConfirm() {
      console.log(this.enclosurefileList);
      return;
      this.$refs['elForm'].validate(valid => {
        if (!valid) return;
        register(this.formData);
        this.close()
      })
    },
    enclosureBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 50
      if (!isRightSize) {
        this.$message.error('文件大小超过 50MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
